<title>教师管理</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a><cite>用户管理</cite></a>
        <a><cite>教师管理</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">教师列表</div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="teacher-table-page" lay-filter="teacher-page"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="lineBar">
    <a class="layui-btn layui-btn-xs" lay-event="reset_password">重置密码</a>
</script>

<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>

<script>
    layui.use(['jquery', 'table'], function(){
        var $ = layui.$, form = layui.form,
            table = layui.table, admin = layui.admin,
            view = layui.view;
        var width = window.screen.availWidth > 420 ? 420 : window.screen.availWidth;
        var tablelns = table.render({
            elem: '#teacher-table-page'
            ,url: "/admin/user/manager"
            ,method: 'get'
            ,where: {userType:1}
            ,cols: [[
                {field:'id', width:'10%',  title: 'ID', align: 'center'}
                ,{field:'userName', width:'20%', title:'用户名', align:'center'}
                ,{field:'createdTime', width:'30%', title: '注册时间', align: 'center', templet: function (d) {
                        return time(d.createdTime);
                    }}
                ,{field:'lastLoginTime', width:'30%', title:'最近登录', align:'center', templet: function (d) {
                        if (d.lastLoginTime == 0){
                            return "未登录"
                        }else {
                            return time(d.lastLoginTime);
                        }
                    }}
                ,{align:'center', width:'10%', title:'操作', toolbar:'#lineBar'}
            ]]
            ,page: true
            ,parseData: function(res) {
                return {
                    "status": res.status,
                    "message": res.message,
                    "total": res.data.total,
                    "list": res.data.content
                };
            }
            ,response: {
                statusName: 'status'
                ,statusCode: 200
                ,msgName: 'message'
                ,countName: 'total'
                ,dataName: 'list'
            }
            ,toolbar: '#toolbar'
        });
        table.on('tool(teacher-page)', function (obj) {
            switch (obj.event) {
                case 'reset_password':
                    let load = layer.load(2);
                    console.log(obj);
                    $.ajax({
                        url:        '/admin/user/manager/reset',
                        data:       {'userName': obj.data.userName},
                        type:       'POST',
                        complete    :function (result) {
                            layer.close(load);
                            result = result.responseJSON;
                            showMsg(result.message);
                        }
                    })
                    break;
            }
        });

        table.on('toolbar(teacher-page)', function (obj) {
            switch (obj.event) {
                case 'add':
                    admin.popup({
                        title: "添加教师",
                        area: [width + "px", "450px"],
                        id: "LAY-popup-user-edit",
                        success: function(layero, index) {
                            view(this.id).render("menu/user/add_teacher").done(function() {
                                form.on("submit(LAY-user-back-submit)", function(data) {
                                    let field = data.field;
                                    let username = field.username;
                                    let password = field.password;
                                    if (!valid(username, password)) {
                                        return;
                                    }

                                    let load = layer.load(2);
                                    let postData = {
                                        username: username,
                                        password: password
                                    };
                                    $.ajax({
                                        url:        '/admin/user/manager/teacher',
                                        data:       postData,
                                        type:       'POST',
                                        complete    :function (result) {
                                            layer.close(load);
                                            result = result.responseJSON;
                                            let status = result.status;
                                            if (status !== 200){
                                                showMsg(result.message);
                                            }else {
                                                showMsg("添加成功")
                                            }
                                        }
                                    })

                                })
                            })
                        },
                        end: function () {
                            layui.index.render();
                        }
                    });
                    break;
            }
        });
    });

    function time(time = +new Date()) {
        var date = new Date(time + 8 * 3600 * 1000); // 增加8小时
        return date.toJSON().substr(0, 19).replace('T', ' ');
    }

    function valid(username, password) {
        if (username == null || username == ""){
            showMsg("请填写用户名");
            return false;
        }
        if (password == null || password === "") {
            showMsg("请填写密码");
            return false;
        }
        if (username.length < 5 || username.length > 30) {
            showMsg("用户名长度应为5-30字符");
            return false;
        }
        if (password.length < 6 || password.length > 30) {
            showMsg("密码长度应为6-30字符");
            return false;
        }
        return true;
    }

    //显示结果
    function showMsg(text) {
        layer.msg("<div style=\"padding: 20px 100px;\">"+ text + "</div>", {
            time: 8000, //8s后自动关闭
            btn: ['明白了'],
            shade: 0.3,
            yes: function (index, layero) {
                layer.close(index);
            }
        });
    }
</script>